package com.adinnet.admin.organization.controller;


import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.organization.model.Organization;
import com.adinnet.admin.organization.model.vo.OrganizationResp;
import com.adinnet.admin.organization.service.OrganizationService;
import com.adinnet.admin.school.model.School;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author liukp
 * @since 2020-12-05
 */
@Controller
@RequestMapping("/sys/organization")
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private CommonService commonService;

    @RequestMapping()
    public String organization() {
        return "organization/organization.html";
    }


    /**
     * 查询所有机构
     **/
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<OrganizationResp> list(String keyword,Integer page, Integer limit) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        return organizationService.organizationList(keyword,page,limit);
    }

    /**
     * 添加机构
     */
    @RequestMapping("/add")
    @ResponseBody
    @Transactional
    public JsonResult add(Organization organization, HttpServletRequest request) {
        QueryWrapper<Organization> qw = new QueryWrapper<>();
        qw.eq("deleted", 0).eq("organization_name", organization.getOrganizationName());
        List<Organization> organizationList = organizationService.list(qw);
        if (CollectionUtils.isNotEmpty(organizationList)) {
            return JsonResult.error("机构名称已存在，请更换名称!");
        }
        if (organizationService.save(organization)) {
            commonService.saveOperation(request,"新增机构，机构ID："+organization.getId());
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改机构
     */
    @RequestMapping("/update")
    @ResponseBody
    @Transactional
    public JsonResult update(Organization organization,HttpServletRequest request) {
        QueryWrapper<Organization> qw = new QueryWrapper<>();
        qw.eq("deleted", 0).eq("organization_name", organization.getOrganizationName()).notIn("id",organization.getId());
        List<Organization> organizationList = organizationService.list(qw);
        if (CollectionUtils.isNotEmpty(organizationList)) {
            return JsonResult.error("机构名称已存在，请更换名称!");
        }
        if (organizationService.updateById(organization)) {
            commonService.saveOperation(request,"修改机构，机构ID："+organization.getId());
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除机构
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Transactional
    public JsonResult delete(Integer organizationId, HttpServletRequest request) {
        commonService.saveOperation(request, "删除机构，机构ID：" + organizationId);
        return organizationService.delete(organizationId);
    }

}
